using Coding4Fun.Phone.Controls.Primitives;
using Microsoft.Phone.Controls;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
namespace Coding4Fun.Phone.Controls.Toolkit
{
	[TemplatePart(Name = "ValueButton", Type = typeof(ButtonBase))]
	public abstract class ValuePickerBase<T> : Control where T : struct
	{
		private const string string_0 = "ValueButton";
		private ButtonBase buttonBase_0;
		private PhoneApplicationFrame phoneApplicationFrame_0;
		private object object_0;
		private NavigationInTransition navigationInTransition_0;
		private NavigationOutTransition navigationOutTransition_0;
		private IValuePickerPage<T> ivaluePickerPage_0;
		private EventHandler<ValueChangedEventArgs<T>> eventHandler_0;
		public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register("DialogTitle", typeof(string), typeof(ValuePickerBase<T>), new PropertyMetadata(""));
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(T?), typeof(ValuePickerBase<T>), new PropertyMetadata(null, new PropertyChangedCallback(ValuePickerBase<T>.smethod_0)));
		public static readonly DependencyProperty ValueStringProperty = DependencyProperty.Register("ValueString", typeof(string), typeof(ValuePickerBase<T>), null);
		public static readonly DependencyProperty ValueStringFormatProperty = DependencyProperty.Register("ValueStringFormat", typeof(string), typeof(ValuePickerBase<T>), new PropertyMetadata(null, new PropertyChangedCallback(ValuePickerBase<T>.OnValueStringFormatChanged)));
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ValuePickerBase<T>), null);
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(ValuePickerBase<T>), null);
		public static readonly DependencyProperty PickerPageUriProperty = DependencyProperty.Register("PickerPageUri", typeof(Uri), typeof(ValuePickerBase<T>), null);
		public event EventHandler<ValueChangedEventArgs<T>> ValueChanged
		{
			add
			{
				EventHandler<ValueChangedEventArgs<T>> eventHandler = this.eventHandler_0;
				EventHandler<ValueChangedEventArgs<T>> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<ValueChangedEventArgs<T>> eventHandler3 = (EventHandler<ValueChangedEventArgs<T>>)Delegate.Combine(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<ValueChangedEventArgs<T>>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
			remove
			{
				EventHandler<ValueChangedEventArgs<T>> eventHandler = this.eventHandler_0;
				EventHandler<ValueChangedEventArgs<T>> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<ValueChangedEventArgs<T>> eventHandler3 = (EventHandler<ValueChangedEventArgs<T>>)Delegate.Remove(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<ValueChangedEventArgs<T>>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
		}
		public string DialogTitle
		{
			get
			{
				return (string)base.GetValue(ValuePickerBase<T>.DialogTitleProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.DialogTitleProperty, value);
			}
		}
		[TypeConverter(typeof(TimeTypeConverter))]
		public T? Value
		{
			get
			{
				return (T?)base.GetValue(ValuePickerBase<T>.ValueProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.ValueProperty, value);
			}
		}
		public string ValueString
		{
			get
			{
				return (string)base.GetValue(ValuePickerBase<T>.ValueStringProperty);
			}
			protected set
			{
				base.SetValue(ValuePickerBase<T>.ValueStringProperty, value);
			}
		}
		public string ValueStringFormat
		{
			get
			{
				return (string)base.GetValue(ValuePickerBase<T>.ValueStringFormatProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.ValueStringFormatProperty, value);
			}
		}
		public object Header
		{
			get
			{
				return base.GetValue(ValuePickerBase<T>.HeaderProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.HeaderProperty, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ValuePickerBase<T>.HeaderTemplateProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.HeaderTemplateProperty, value);
			}
		}
		public Uri PickerPageUri
		{
			get
			{
				return (Uri)base.GetValue(ValuePickerBase<T>.PickerPageUriProperty);
			}
			set
			{
				base.SetValue(ValuePickerBase<T>.PickerPageUriProperty, value);
			}
		}
		protected virtual string ValueStringFormatFallback
		{
			get
			{
				return "{0}";
			}
		}
		private static void smethod_0(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((ValuePickerBase<T>)dependencyObject_0).method_0((T?)dependencyPropertyChangedEventArgs_0.get_OldValue(), (T?)dependencyPropertyChangedEventArgs_0.get_NewValue());
		}
		private void method_0(T? nullable_0, T? nullable_1)
		{
			this.UpdateValueString();
			this.OnValueChanged(new ValueChangedEventArgs<T>(nullable_0, nullable_1));
		}
		protected virtual void OnValueChanged(ValueChangedEventArgs<T> valueChangedEventArgs_0)
		{
			EventHandler<ValueChangedEventArgs<T>> eventHandler = this.eventHandler_0;
			if (eventHandler != null)
			{
				eventHandler.Invoke(this, valueChangedEventArgs_0);
			}
		}
		private static void OnValueStringFormatChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((ValuePickerBase<T>)dependencyObject_0).method_1();
		}
		private void method_1()
		{
			this.UpdateValueString();
		}
		public override void OnApplyTemplate()
		{
			if (this.buttonBase_0 != null)
			{
				this.buttonBase_0.remove_Click(new RoutedEventHandler(this.method_2));
			}
			base.OnApplyTemplate();
			this.buttonBase_0 = (base.GetTemplateChild("ValueButton") as ButtonBase);
			if (this.buttonBase_0 != null)
			{
				this.buttonBase_0.add_Click(new RoutedEventHandler(this.method_2));
			}
		}
		private void method_2(object sender, RoutedEventArgs e)
		{
			this.OpenPicker();
		}
		protected internal virtual void UpdateValueString()
		{
			this.ValueString = string.Format(CultureInfo.get_CurrentCulture(), this.ValueStringFormat ?? this.ValueStringFormatFallback, new object[]
			{
				this.Value
			});
		}
		public void OpenPicker()
		{
			if (null == this.PickerPageUri)
			{
				throw new ArgumentException("PickerPageUri property must not be null.");
			}
			if (this.phoneApplicationFrame_0 == null)
			{
				this.phoneApplicationFrame_0 = (Application.get_Current().get_RootVisual() as PhoneApplicationFrame);
				if (this.phoneApplicationFrame_0 != null)
				{
					this.object_0 = this.phoneApplicationFrame_0.get_Content();
					UIElement uIElement = this.object_0 as UIElement;
					if (uIElement != null)
					{
						this.navigationInTransition_0 = TransitionService.GetNavigationInTransition(uIElement);
						TransitionService.SetNavigationInTransition(uIElement, null);
						this.navigationOutTransition_0 = TransitionService.GetNavigationOutTransition(uIElement);
						TransitionService.SetNavigationOutTransition(uIElement, null);
					}
					this.phoneApplicationFrame_0.add_Navigated(new NavigatedEventHandler(this.method_4));
					if (this.phoneApplicationFrame_0.GetType() == typeof(PhoneApplicationFrame))
					{
						this.phoneApplicationFrame_0.add_NavigationStopped(new NavigationStoppedEventHandler(this.method_5));
					}
					this.phoneApplicationFrame_0.add_NavigationFailed(new NavigationFailedEventHandler(this.method_5));
					this.phoneApplicationFrame_0.Navigate(this.PickerPageUri);
				}
			}
		}
		private void method_3()
		{
			if (this.phoneApplicationFrame_0 != null)
			{
				this.phoneApplicationFrame_0.remove_Navigated(new NavigatedEventHandler(this.method_4));
				this.phoneApplicationFrame_0.remove_NavigationStopped(new NavigationStoppedEventHandler(this.method_5));
				this.phoneApplicationFrame_0.remove_NavigationFailed(new NavigationFailedEventHandler(this.method_5));
				UIElement uIElement = this.object_0 as UIElement;
				if (uIElement != null)
				{
					TransitionService.SetNavigationInTransition(uIElement, this.navigationInTransition_0);
					this.navigationInTransition_0 = null;
					TransitionService.SetNavigationOutTransition(uIElement, this.navigationOutTransition_0);
					this.navigationOutTransition_0 = null;
				}
				this.phoneApplicationFrame_0 = null;
				this.object_0 = null;
			}
			if (this.ivaluePickerPage_0 != null)
			{
				if (this.ivaluePickerPage_0.Value.get_HasValue())
				{
					this.Value = new T?(this.ivaluePickerPage_0.Value.get_Value());
				}
				this.ivaluePickerPage_0 = null;
			}
		}
		private void method_4(object sender, NavigationEventArgs e)
		{
			if (e.get_Content() == this.object_0)
			{
				this.method_3();
				return;
			}
			if (this.ivaluePickerPage_0 == null)
			{
				this.ivaluePickerPage_0 = (e.get_Content() as IValuePickerPage<T>);
				if (this.ivaluePickerPage_0 != null)
				{
					this.NavigateToNewPage(e.get_Content());
				}
			}
		}
		protected virtual void NavigateToNewPage(object page)
		{
			IValuePickerPage<T> valuePickerPage = page as IValuePickerPage<T>;
			if (valuePickerPage == null)
			{
				return;
			}
			valuePickerPage.Value = new T?(this.Value.GetValueOrDefault());
			valuePickerPage.DialogTitle = this.DialogTitle;
		}
		private void method_5(object sender, EventArgs e)
		{
			this.method_3();
		}
	}
}
